﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
namespace spaceVibration
{
    
    class DspFunction
    {
       
        /********雷航提供的参数，命令字定义*************/
        //头部2      //板卡2     //命令2     //数据类型2 //数据长度4              //数据地址4             //保留2     //校验码2
        byte[] NETCMD_HEARTBEAT = { 0x5a, 0x5a, 0x78, 0x56, 0x78, 0x78, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x04, 0x38, 0x38, 0x78, 0x56 };
        byte[] NETCMD_OPEN = { 0x5a, 0x5a, 0x78, 0x56, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x04, 0x38, 0x38, 0x78, 0x56 };	// 打开板卡  cmd+boardtype+boardindex     / ack 
        byte[] NETCMD_CLOSE = { 0x5a, 0x5a, 0x78, 0x56, 0x37, 0x37, 0x01, 0x01, 0x31, 0x31, 0x31, 0x31, 0x31, 0x32, 0x33, 0x34, 0x38, 0x38, 0x78, 0x56 };    // 关闭板卡  cmd+boardtype+boardindex     / ack 
        byte[] NETCMD_READ = { 0x5a, 0x5a, 0x78, 0x56, 0x8D, 0x8D, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x04, 0x38, 0x38, 0x78, 0x56 };	// 读取数据  cmd+boardtype+boardindex+dspindex+length+stride+dspaddr ack(+data)
        //byte[] NETCMD_READ_INT =  { 0x5a, 0x5a, 0x78, 0x56, 0x8D, 0x8D, 0x3C, 0x3C, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x04, 0x38, 0x38, 0x78, 0x56 };	// 读取数据  cmd+boardtype+boardindex+dspindex+length+stride+dspaddr ack(+data)
        //byte[] NETCMD_READ_FLOAT ={ 0x5a, 0x5a, 0x78, 0x56, 0x8D, 0x8D, 0x62, 0x63, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x04, 0x38, 0x38, 0x78, 0x56 };	// 读取数据  cmd+boardtype+boardindex+dspindex+length+stride+dspaddr ack(+data)
        byte[] NETCMD_WRITE = { 0x5a, 0x5a, 0x78, 0x56, 0xFA, 0xFA, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x04, 0x38, 0x38, 0x78, 0x56 };    // 写入数据  cmd+boardtype+boardindex+dspindex+length+stride+dspaddr+buffer /ack 
        byte[] NETCMD_RESET = { 0x5a, 0x5a, 0x78, 0x56, 0x8E, 0x8E, 0x01, 0x01, 0x81, 0x81, 0x81, 0x81, 0x81, 0x82, 0x83, 0x84, 0x38, 0x38, 0x78, 0x56 };    // 复位板卡  cmd+boardtype+boardindex     / ack
        byte[] NETCMD_BOOT = { 0x5a, 0x5a, 0x78, 0x56, 0x92, 0x92, 0x01, 0x01, 0x91, 0x91, 0x91, 0x91, 0x91, 0x92, 0x93, 0x94, 0x38, 0x38, 0x78, 0x56 };	// 加载程序  cmd+boardtype+boardindex+dspindex+length +buffer  / ack
        byte[] NETCMD_BREAK = { 0x5a, 0x5a, 0x78, 0x56, 0x6B, 0x6B, 0x01, 0x01, 0x61, 0x61, 0x61, 0x61, 0x61, 0x62, 0x63, 0x64, 0x38, 0x38, 0x78, 0x56 };	// 请求断纤数据  cmd+boardtype+boardindex+dspindex+length +buffer  / ack
        byte[] NETCMD_ALARM = { 0x5a, 0x5a, 0x78, 0x56, 0x9D, 0x9D, 0x01, 0x01, 0x91, 0x91, 0x91, 0x91, 0x91, 0x92, 0x93, 0x94, 0x38, 0x38, 0x78, 0x56 };	// 请求预警  cmd+boardtype+boardindex+dspindex+length +buffer  / ack
        byte[] NETCMD_SIGNAL = { 0x5a, 0x5a, 0x78, 0x56, 0xBF, 0xBF, 0x01, 0x01, 0xB1, 0xB1, 0xB1, 0xB1, 0xB1, 0xB2, 0xB3, 0xB4, 0x38, 0x38, 0x78, 0x56 };	// 请求语音  cmd+boardtype+boardindex+dspindex+length +buffer  / ack
        
        byte[] NETCMD_OTDR = { 0x5a, 0x5a, 0x78, 0x56, 0x72, 0x72, 0x01, 0x01, 0x71, 0x71, 0x71, 0x71, 0x71, 0x72, 0x73, 0x74, 0x38, 0x38, 0x78, 0x56 };	// 请求OTDR  cmd+boardtype+boardindex+dspindex+length +buffer  / ack
        byte[] NETCMD_WRITE_CA = { 0x5a, 0x5a, 0x78, 0x56, 0xFA, 0xFA, 0x01, 0x01, 0x00, 0x7D, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4, 0x38, 0x38, 0x78, 0x56 };//写入门限
        //byte[] NETCMD_WRITE_SPECIAL = { 0x5a, 0x5a, 0x78, 0x56, 0x82, 0x82, 0x01, 0x01, 0x57, 0x63, 0x57, 0x63, 0xD0, 0x07, 0x00, 0x00, 0x38, 0x38, 0x78, 0x56 };//写入特殊参数，先写入尾纤2000
        byte[] NETCMD_WRITE_SPECIAL = { 0x5a, 0x5a, 0x78, 0x56, 0xEA, 0xEA, 0x01, 0x01, 0xAA, 0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xBB, 0x38, 0x38, 0x78, 0x56 };

        public DateTime timeofLastHeartBeat= new System.DateTime();

        public const string DSP_IP = "192.168.1.38";

        public const int DSP_PORT = 1007;

        const int NETCMD_LENGTH = 20;
        const int NETCMD_DATA_LENGTH_START = 8;//第八位
        const int NETCMD_DATA_TYPE_START = 6;
        const int NETCMD_DATA_ADDR_START = 12;
        const int NETCMD_RET_START = 4;
        const int NETCMD_SPECIAL_NAME_START = 8;
        const int NETCMD_RESERVED_START = 16;
        const int NETCMD_SPECIAL_VALUE_START = 12;
        const int STREAM_BUFFER_LENGTH = 1024;
        const int STREAM_OTDR_BUFFER_LENGTH = 1000;
        
        byte[] NETCMD_RET_OPEN_SUCCESS = { 0x01, 0x01 }; byte[] NETCMD_RET_OPEN_FAIL = { 0x02, 0x02 }; byte[] NETCMD_RET_OPEN_ERROR = { 0x03, 0x03 };
        byte[] NETCMD_RET_CLOSE_SUCCESS = { 0x31, 0x31 }; byte[] NETCMD_RET_CLOSE_FAIL = { 0x32, 0x32 }; byte[] NETCMD_RET_CLOSE_ERROR = { 0x33, 0x33 };
        byte[] NETCMD_RET_READ_SUCCESS = { 0x81, 0x81 }; byte[] NETCMD_RET_READ_FAIL = { 0x82, 0x82 }; byte[] NETCMD_RET_READ_ERROR = { 0x83, 0x83 };
        byte[] NETCMD_RET_WRITE_SUCCESS = { 0xF1, 0xF1 }; byte[] NETCMD_RET_WRITE_FAIL = { 0xF2, 0xF2 }; byte[] NETCMD_RET_WRITE_ERROR = { 0xF3, 0xF3 };
        byte[] NETCMD_RET_RESET_SUCCESS = { 0x81, 0x81 }; byte[] NETCMD_RET_RESET_FAIL = { 0x82, 0x82 }; byte[] NETCMD_RET_RESET_ERROR = { 0x83, 0x83 };
        byte[] NETCMD_RET_BOOT_SUCCESS = { 0x91, 0x91 }; byte[] NETCMD_RET_BOOT_FAIL = { 0x92, 0x92 }; byte[] NETCMD_RET_BOOT_ERROR = { 0x93, 0x93 };
        byte[] NETCMD_RET_BREAK_SUCCESS = { 0xB1, 0xB1 }; byte[] NETCMD_RET_BREAK_FAIL = { 0xB2, 0xB2 }; byte[] NETCMD_RET_BREAK_ERROR = { 0xB3, 0xB3 };
        byte[] NETCMD_RET_ALARM_SUCCESS = { 0xD1, 0xD1 }; byte[] NETCMD_RET_ALARM_FAIL = { 0xD2, 0xD2 }; byte[] NETCMD_RET_ALARM_ERROR = { 0xD3, 0xD3 };
        byte[] NETCMD_RET_SIGNAL_SUCCESS = { 0xF1, 0xF1 };
        byte[] NETCMD_RET_OTDR_SUCCESS = { 0x21, 0x21 }; byte[] NETCMD_RET_OTDR_FAIL = { 0x22, 0x22 }; byte[] NETCMD_RET_OTDR_ERROR = { 0x23, 0x23 };
        byte[] NETCMD_RET_SPECIAL_SUCCESS = { 0xA1, 0xA1 }; byte[] NETCMD_RET_SPECIAL_FAIL = { 0xA2, 0xA2 }; byte[] NETCMD_RET_SPECIAL_ERROR = { 0xA3, 0xA3 };
        byte[] NETCMD_RET_ALARM_END = { 0x96, 0x96 };
         byte[] NETCMD_RET_SIGNAL_END = { 0xBE, 0xBE };
        byte[] NETCMD_RET_OTDR_END = { 0x6E, 0x6E };
        byte[] NETCMD_RET_WRITE_END = { 0xF1, 0xF1 };
        byte[] NETCMD_DSP_FLASH_ADDR = { 0x00,0x00,0x00,0x00 };
        public byte[] NETCMD_SPECIAL_NAME_KSPS = {  0x68, 0x39, 0x68,0x39 }, NETCMD_SPECIAL_NAME_WORKCYCLE = {  0x23, 0x57, 0x23, 0x57 }, NETCMD_SPECIAL_NAME_PERIODCYCLE = { 0x39, 0x66, 0x39, 0x66 }, NETCMD_SPECIAL_NAME_BOARDCODE = {  0x96, 0x25, 0x96, 0x25 }, NETCMD_SPECIAL_NAME_BREAK = {0x63, 0x57, 0x63, 0x57}, NETCMD_SPECIAL_NAME_SINGLECOL = { 0x52, 0x69, 0x52, 0x69 };
        public byte[] NETCMD_SPECIAL_NAME_SINGLECOL_1 = { 0x51, 0x69, 0x51, 0x69 };
        public byte[] NETCMD_SPECIAL_NAME_SINGLECOL_2 = { 0x52, 0x69, 0x52, 0x69 };
        public byte[] NETCMD_SPECIAL_NAME_SINGLECOL_3 = { 0x53, 0x69, 0x53, 0x69 };
        //byte[] NETCMD_RET_BREAK_SUCCESS = { 0x61, 0x61 }; byte[] NETCMD_RET_BREAK_FAIL = { 0x62, 0x62 }; byte[] NETCMD_RET_BREAK_ERROR = { 0x63, 0x63 };
        //byte[] NETCMD_RET_ALARM_SUCCESS = { 0x91, 0x91 }; byte[] NETCMD_RET_ALARM_FAIL = { 0x92, 0x92 }; byte[] NETCMD_RET_ALARM_ERROR = { 0x93, 0x93 };
        //byte[] NETCMD_RET_OTDR_SUCCESS = { 0x71, 0x71 };  byte[] NETCMD_RET_OTDR_FAIL = { 0x72, 0x72 };  byte[] NETCMD_RET_OTDR_ERROR = { 0x73, 0x73 };
        //命令执行流程
        //打开板卡-》复位板卡-》加载板卡 -》读取/写入板卡-》关闭板卡
        public readonly object sync = new object();
        public TcpClient client = null;                 //为TCP网络提供客户端连接
        public NetworkStream stream = null;             //提供用于网络访问的基础数据流
        public byte[] DATA_TYPE_INT = { 0x3C, 0x3C };
        public byte[] DATA_TYPE_ANY = { 0x01, 0x01 };
        public byte[] DATA_TYPE_FLOAT = { 0x62, 0x63 };
        byte[] file_byte_Length;
        byte[] file_byte_msg_crc;

        //存储原始数据文件名
        public static string timeNow = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒");
        public static string fn = timeNow + ".wav";
        public static string ph;
        public static int insertOriginDataFlag = 0;

        /*******************************雷航提供程序**************************************/
        public bool DevBoot(uint nBoardIndex, string strFileName, uint DSP_ROM_Addr)
        {
            try
            {
                if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        //int retAck = -1;
                        //DevReClose(0);
                        FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read);
                        long filelength = fs.Length;
                        BinaryReader br = new BinaryReader(fs);

                        byte[] filebyte = br.ReadBytes((int)filelength);
                        fs.Close();
                        br.Close();
     //                   if (((int)filelength % 4) != 0)
     //                   {
     //                       return false;
      //                  }

                        //写入数据
                        //write data 
                        int bLength = (int)filelength;
                        //send cmd
                        byte[] byte_array_addr = BitConverter.GetBytes(DSP_ROM_Addr);
                        file_byte_Length = BitConverter.GetBytes(bLength);
                        DATA_TYPE_INT.CopyTo(NETCMD_WRITE_CA, NETCMD_DATA_TYPE_START);
                        file_byte_Length.CopyTo(NETCMD_WRITE_CA, NETCMD_DATA_LENGTH_START);
                        byte_array_addr.CopyTo(NETCMD_WRITE_CA, NETCMD_DATA_ADDR_START);
                        file_byte_msg_crc = CRC16(filebyte);
                        file_byte_msg_crc.CopyTo(NETCMD_WRITE_CA, NETCMD_RESERVED_START);
                        stream.Write(CRC_ENCODE(NETCMD_WRITE_CA), 0, NETCMD_LENGTH);
                        printByte(NETCMD_WRITE_CA);
                        stream.Flush();
                        //rec ack and get datalength
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_WRITE_SUCCESS, ref ret_cmd_ack_result)) { d("DevWriteCA CMD 错误"); return false; }

                        for (int i = bLength; i > 0; i -= STREAM_BUFFER_LENGTH)
                        {
                            Thread.Sleep(10);
                            int one_lenth = STREAM_BUFFER_LENGTH;
                            if (i > 0 && i < STREAM_BUFFER_LENGTH) one_lenth = i;
                            stream.Write(filebyte, bLength - i, one_lenth);
                        }
                        stream.Flush();

                        byte[] recvEndACK = new byte[NETCMD_LENGTH];
                        ret = stream.Read(recvEndACK, 0, NETCMD_LENGTH);
                        printByte(recvEndACK);
                        byte[] byte_ret_msg_crc = new byte[2];
                        byte_ret_msg_crc[0] = recvEndACK[NETCMD_RESERVED_START];
                        byte_ret_msg_crc[1] = recvEndACK[NETCMD_RESERVED_START + 1];
                        if (!byteIsEqual(byte_ret_msg_crc, file_byte_msg_crc)) { d("DevWriteCA 摘要 错误"); return false; }
                        //第二次返回
                        if (!CRC_DECODE(recvEndACK, NETCMD_RET_WRITE_END, ref ret_cmd_ack_result)) { d("DevWriteCA END 错误"); return false; }

                        
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);//连接失败
                return false;
            }
            return true;

           
           

        }
        public bool DevBootCMDSEND() {
            try
            {
                 if (!client.Connected) { DevTCP(0,  DSP_PORT); }
                {
                    lock (sync)
                    {
                        file_byte_Length.CopyTo(NETCMD_BOOT, NETCMD_DATA_LENGTH_START);
                        NETCMD_DSP_FLASH_ADDR.CopyTo(NETCMD_BOOT, NETCMD_DATA_ADDR_START);
                        file_byte_msg_crc.CopyTo(NETCMD_BOOT, NETCMD_RESERVED_START);
                        stream.Write(CRC_ENCODE(NETCMD_BOOT), 0, NETCMD_LENGTH);//thomas
                        printByte(NETCMD_BOOT);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);//连接失败
                return false;
            }
            return true;
        }

        public bool GetBootResult() {
            try
            {
                if (!client.Connected) { DevTCP(0,  DSP_PORT); }
                {
                    lock (sync)
                    {
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);//连接失败
                return false;
            }
            return true;
        }
        public bool DevTCP(uint nBoardIndex,  int PORT)
        {
           
            try
            {
                lock (sync)
                {

                    
                   d("Waiting For DSP Connection ");
                     
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);//连接失败
                return false;
            }
            return true;
        }

        public bool DevOpen(uint nBoardIndex, string IP, int PORT)
        {
            //if (client != null && stream!=null &&) { stream.Close(); client.Close(); }
            try
            {
                lock (sync)
                {
                    if (!client.Connected) { DevTCP(nBoardIndex, DSP_PORT); }
                    d("Try Connecting DSP ");
                    //client = new TcpClient(IP, PORT);//实例化TcpClient
                    //stream = client.GetStream();//获取网络流
                                                /*stream.Write(CRC_ENCODE_TEMP(NETCMD_OPEN), 0, NETCMD_LENGTH);*///临时
                    stream.Write(CRC_ENCODE(NETCMD_OPEN), 0, NETCMD_LENGTH);//将数据写入网络流,thomas
                                                                            // stream.Flush();
                    printByte(NETCMD_OPEN);
                    d(string.Format("Connect TCP IP={0},PORT={1} Successfully", IP, PORT));

                    //读取应答
                    byte[] recvACK = new byte[NETCMD_LENGTH];
                    int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                    printByte(recvACK);
                    byte[] ret_cmd_ack_result = new byte[2];
                    if (!CRC_DECODE(recvACK, NETCMD_RET_OPEN_SUCCESS, ref ret_cmd_ack_result))
                    {
                        d("DevOpen 已经打开过");
                        //if (byteIsEqual(ret_cmd_ack_result,NETCMD_RET_OPEN_ERROR)) DevReClose(0);
                        return true;
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);//连接失败
                return false;
            }
            return true;
        }
        public bool DevHeartBeatReceive()
        {
            try
            {
                lock (sync)
                {
                    if (!client.Connected) { DevTCP(0, DSP_PORT); }
                    d("Receiving DSP HeartBeat ");
                    //读取应答
                    //byte[] recvHeartBeat = new byte[NETCMD_LENGTH];
                    //int ret = stream.Read(recvHeartBeat, 0, NETCMD_LENGTH);
                   
                       
                   
                    //// printByte(recvHeartBeat);
                    //timeofLastHeartBeat = System.DateTime.Now;
                    //Thread.Sleep(10);
                    //if (ret != 0)
                        stream.Write(CRC_ENCODE(NETCMD_HEARTBEAT), 0, NETCMD_LENGTH);//将数据写入网络流,thomas
                                                                            // stream.Flush();
                    printByte(NETCMD_HEARTBEAT);
                    d(string.Format("Send HeartBeat2DSP Successfully"));

                }
                Thread.Sleep(2000);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);//连接失败
                Thread.Sleep(1000);
                return false;
            }
            return true;
        }
        public void printByte(byte[] byte_arr)
        {
            for (int i = 0; i < byte_arr.Length; i++)
            {
                string a = Convert.ToString(byte_arr[i], 16);
                if (byte_arr[i] < 0x10) Console.Write("0" + a + " ");
                else Console.Write(a + " ");
            }
            Console.WriteLine();
        }
        public bool DevReClose(uint nBoardIndex)
        {
            try
            {
                if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        d("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!ReClose DSP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                        stream.Flush();
                        stream.Write(CRC_ENCODE(NETCMD_CLOSE), 0, NETCMD_LENGTH);//thomas
                        byte[] recvCloseACK = new byte[NETCMD_LENGTH];
                        int retClose = stream.Read(recvCloseACK, 0, NETCMD_LENGTH);
                        printByte(recvCloseACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvCloseACK, NETCMD_RET_CLOSE_SUCCESS, ref ret_cmd_ack_result)) { d("DevReOpen Close 错误"); return false; }
                        stream.Flush();
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);//连接失败
                return false;
            }
            return true;

        }
        public bool DevBootReset(uint nBoardIndex)
        {
           // printByte(CRC_ENCODE(NETCMD_RESET));
            try
            {
                if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        d("!!!!!!!!!!!!!!!!!系统重置!!!!!!");
                        stream.Flush();
                        stream.Write(CRC_ENCODE(NETCMD_RESET), 0, NETCMD_LENGTH);//thomas
                        byte[] recvCloseACK = new byte[NETCMD_LENGTH];
                        int retClose = stream.Read(recvCloseACK, 0, NETCMD_LENGTH);
                        printByte(recvCloseACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvCloseACK, NETCMD_RET_CLOSE_SUCCESS, ref ret_cmd_ack_result)) { d("DevBootReset Close 错误"); return false; }
                        stream.Flush();
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);//连接失败
                return false;
            }
            return true;

        }
        public bool DevReOpen(uint nBoardIndex)
        {
            try
            {
                 if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        d("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!ReConnecting DSP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                        stream.Write(CRC_ENCODE(NETCMD_OPEN), 0, NETCMD_LENGTH);//临时
                                                                                //stream.Write(CRC_ENCODE(NETCMD_OPEN), 0, NETCMD_LENGTH);//将数据写入网络流,thomas
                                                                                // stream.Flush();
                                                                                //读取应答
                        stream.Flush();
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        //int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_OPEN_SUCCESS, ref ret_cmd_ack_result))
                        {
                            d("DevReOpen Open 错误");
                            return false;
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);//连接失败
                return false;
            }
            return true;
        }

        // 关闭板卡
        public bool DevClose(uint nBoardIndex)
        {
            try
            {
                if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        //stream.Write(CRC_ENCODE_TEMP(NETCMD_CLOSE), 0, NETCMD_LENGTH);//临时
                        stream.Write(CRC_ENCODE(NETCMD_CLOSE), 0, NETCMD_LENGTH);//thomas
                        printByte(NETCMD_CLOSE);
                        stream.Flush();
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        stream.Close();
                        client.Close();
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_CLOSE_SUCCESS, ref ret_cmd_ack_result)) { d("DevClose 错误"); return false; }
                    }
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }
            return true;
        }
        // 复位板卡
        public bool DevReset(uint nBoardIndex)
        {
            try
            {
                 if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        stream.Write(CRC_ENCODE(NETCMD_RESET), 0, NETCMD_LENGTH);//临时
                                                                                 //stream.Write(CRC_ENCODE(NETCMD_RESET), 0, NETCMD_LENGTH);//thomas
                        stream.Flush();
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_RESET_SUCCESS, ref ret_cmd_ack_result)) { d("DevReset 错误"); return false; }
                    }
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }
            return true;
        }

        // 读取数据
        public bool DevReadAlarm(uint nBoardIndex, ref int[] FinalAlarm, ref int dataLength)
        {
            int bLength = 0;
            try
            {
                if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        //send cmd
                        Thread.Sleep(5);
                        stream.Write(CRC_ENCODE(NETCMD_ALARM), 0, NETCMD_LENGTH);//thomas
                        printByte(NETCMD_ALARM);
                        stream.Flush();
                        //rec ack and get datalength
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_ALARM_SUCCESS, ref ret_cmd_ack_result)) { d("DevReadAlarm 错误"); return false; }
                        //dLength是一个32位无符号整形，小端模式
                        bLength = (int)recvACK[NETCMD_DATA_LENGTH_START + 3] << 24;
                        bLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 2] << 16;
                        bLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 1] << 8;
                        bLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 0];

                        Thread.Sleep(5);
                        //recv data 
                        byte[] recvData = new byte[bLength];
                        for (int i = bLength; i > 0; i -= STREAM_BUFFER_LENGTH)
                        {
                            Thread.Sleep(5);
                            int one_lenth = STREAM_BUFFER_LENGTH;
                            if (i > 0 && i < STREAM_BUFFER_LENGTH) one_lenth = i;
                            stream.Read(recvData, bLength - i, one_lenth);
                        }
                        stream.Flush();
                        dataLength = bLength / 4;
                        FinalAlarm = new int[dataLength];
                        for (int i = 0; i < dataLength; i++)
                        {
                            FinalAlarm[i] = (int)BitConverter.ToUInt32(recvData, i * 4);
                        }
                        Thread.Sleep(5);
                        byte[] recvEndACK = new byte[NETCMD_LENGTH];
                        ret = stream.Read(recvEndACK, 0, NETCMD_LENGTH);
                        printByte(recvEndACK);
                        //第二次读取返回数据
                        if (!CRC_DECODE(recvEndACK, NETCMD_RET_ALARM_END, ref ret_cmd_ack_result))
                        {
                            //if (recvEndACK[0] == 0x5a && recvEndACK[1] == 0x5a && recvEndACK[2] == 0x34 && recvEndACK[3] == 0x12) return true;
                            d("DevReadAlarm 错误");
                            return false;
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }

            return true;
        }
        // 读取语音
        public bool DevSignalCol(uint nBoardIndex,ref int[] SingalUint, ref byte[] Signalbyte, ref int dataLength)
        {
            int bLength = 0;
            try
            {
                if (!client.Connected) { DevTCP(nBoardIndex, DSP_PORT); }
                {
                    lock (sync)
                    {
                        //send cmd
                        Thread.Sleep(5);
                        stream.Write(CRC_ENCODE(NETCMD_SIGNAL), 0, NETCMD_LENGTH);//thomas
                        printByte(NETCMD_SIGNAL);
                        stream.Flush();
                        //rec ack and get datalength
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_SIGNAL_SUCCESS, ref ret_cmd_ack_result)) {
                            byte[] tmpRecv = new byte[1024];
                            for(int ii = 0; ii < 100; ii++)
                            {
                                int ret2 = stream.Read(tmpRecv,0,1024);
                                if (ret2 < 1023) break;
                            }
                            d("DevReadAlarm 错误");
                            return false;
                        }
                        //dLength是一个32位无符号整形，小端模式
           
                        bLength = (int)BitConverter.ToUInt32(recvACK, NETCMD_DATA_LENGTH_START);

                        Thread.Sleep(5);
                        //recv data 
                        byte[] recvData = new byte[bLength];
                        for (int i = bLength; i > 0; i -= STREAM_BUFFER_LENGTH)
                        {
                            Thread.Sleep(5);
                            int one_lenth = STREAM_BUFFER_LENGTH;
                            if (i > 0 && i < STREAM_BUFFER_LENGTH) one_lenth = i;
                            stream.Read(recvData, bLength - i, one_lenth);
                        }
                        stream.Flush();
                        int  dataLengthn = bLength / 4;
                        
                        Signalbyte = new byte[2 * dataLengthn];
                        //SingalUint = new int [dataLengthn];
                        //兼容五个通道，航天项目只保存三个通道的数据
                        byte[] shuzu = new byte[Signalbyte.Length/ 5];
                        //for (int i=0; i<3; i++)
                        //{
                        //    Array.Copy(recvData,i*(bLength / 5),shuzu,0,bLength/5); 
                        //    storeByte(shuzu, i);
                        //}

                        for (int i = 0; i < dataLengthn; i++)
                        {
                            UInt32 uSignal = BitConverter.ToUInt32(recvData, 4 * i);
                           
                            uSignal = uSignal >> 1;
                            byte[] arr = BitConverter.GetBytes(uSignal);
                            Signalbyte[2 * i] = arr[0];
                            Signalbyte[2 * i + 1] = arr[1];

                            //SingalUint[i] = (int)uSignal;
                        }
                        //保留原始数据，目前航天只保留一个通道的数据
                        for (int i = 0; i < 1; i++)
                        {
                            Array.Copy(Signalbyte, i * (Signalbyte.Length / 5), shuzu, 0, Signalbyte.Length / 5);
                            storeByte(shuzu, i);
                        }

                        dataLength = dataLengthn * 2;


                        Thread.Sleep(5);
                        byte[] recvEndACK = new byte[NETCMD_LENGTH];
                        ret = stream.Read(recvEndACK, 0, NETCMD_LENGTH);
                        printByte(recvEndACK);
                        //第二次读取返回数据
                    
                        if (!CRC_DECODE(recvEndACK, NETCMD_RET_SIGNAL_END, ref ret_cmd_ack_result) || ( dataLengthn %1024!=0 ))
                        {
                            byte[] tmpRecv = new byte[1024];
                            for (int ii = 0; ii < 100; ii++)
                            {
                                int ret2 = stream.Read(tmpRecv, 0, 1024);
                                if (ret2 < 1023) break;
                            }
                            //if (recvEndACK[0] == 0x5a && recvEndACK[1] == 0x5a && recvEndACK[2] == 0x34 && recvEndACK[3] == 0x12) return true;
                            d("DevReadAlarm 错误");
                            return false;
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }

            return true;
        }
        // 读取数据
        public bool DevReadHistoryAlarm(uint nBoardIndex, ref int[] FinalAlarm, ref int dataLength)
        {

            try
            {   
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }

            return true;
        }
        // 读取数据
        public bool DevReadBreak(uint nBoardIndex, ref int break_position_avg, ref int break_position_max)
        {
            try
            {
                 if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        //send cmd
                        stream.Write(CRC_ENCODE(NETCMD_BREAK), 0, NETCMD_LENGTH);
                        printByte(NETCMD_BREAK);
                        stream.Flush();
                        //rec ack and get datalength
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        Thread.Sleep(20);
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_BREAK_SUCCESS, ref ret_cmd_ack_result)) { d("DevReadBreak 错误"); return false; }
                        //dLength先按 16位进制的数量 理解
                        break_position_max = (int)BitConverter.ToUInt32(recvACK, NETCMD_DATA_ADDR_START);
                        break_position_avg = (int)BitConverter.ToUInt32(recvACK, NETCMD_DATA_LENGTH_START);
                    }
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }
            return true;
        }

        /// <summary>
        /// c.传输全线数据方式
        /// </summary>
        /// <param name="nBoardIndex">板卡编号</param>
        /// <param name="OTDRAlarm">全线数据</param>
        /// <param name="dataLength">数据长度</param>
        /// <returns>返回全线数据</returns>
        public bool DevReadOTDR(uint nBoardIndex, ref int[] OTDRAlarm, int dataLength)
        {
            int bLength = dataLength * 4;
            try
            {
                if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }

                {
                    lock (sync)
                    {
                        //send cmd
                        //stream.Write(CRC_ENCODE_TEMP(NETCMD_OTDR), 0, NETCMD_LENGTH);
                        stream.Write(CRC_ENCODE(NETCMD_OTDR), 0, NETCMD_LENGTH);
                        printByte(NETCMD_OTDR);
                        stream.Flush();
                        //rec ack and get datalength
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_OTDR_SUCCESS, ref ret_cmd_ack_result)) { d("DevReadOTDR CMD ACK 错误"); return false; }
                        //dLength先按 16位进制的数量 理解

                        //recv data 
                        byte[] recvData = new byte[bLength];
                        for (int i = bLength; i > 0; i -= STREAM_OTDR_BUFFER_LENGTH)
                        {
                            Thread.Sleep(5);
                            int one_lenth = STREAM_OTDR_BUFFER_LENGTH;
                            if (i > 0 && i < STREAM_OTDR_BUFFER_LENGTH) one_lenth = i;
                            stream.Read(recvData, bLength - i, one_lenth);
                        }
                        //storeIntToTxt(recvData, "D:\\otdr_hex.txt");
                        stream.Flush();
                        for (int i = 0; i < dataLength; i++)
                        {
                            OTDRAlarm[i] = (int)BitConverter.ToUInt32(recvData, i * 4);
                        }

                        byte[] recvEndACK = new byte[NETCMD_LENGTH];
                        ret = stream.Read(recvEndACK, 0, NETCMD_LENGTH);
                        printByte(recvEndACK);
                        //第二次读取返回结果
                        if (!CRC_DECODE(recvEndACK, NETCMD_RET_OTDR_END, ref ret_cmd_ack_result)) { d("DevReadOTDR END ACK 错误"); return false; }
                    }
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }

            return true;
        }
        private void storeByte(byte[] shuzu,int channel)
        {
            insertOriginDataFlag++;
            TimeSpan span = DateTime.Now - Convert.ToDateTime(timeNow);
            //如果连续存储超过24小时，则重新创建文件
            if (span.TotalHours > 24)
            {
                timeNow = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒");
            }
            //string fn = "all.wav";
            //string ph = @"D:/spaceObject/" + fn;
            ph = @"./spaceObject/"+channel+"/" + fn;
            //DateTime.Now.Date.ToString();
            //FileStream fs = new FileStream(ph, FileMode.Create);//新建文件
            FileStream fs = new FileStream(ph, FileMode.Append);//新建文件
            fs.Write(shuzu, 0, shuzu.Length);
            fs.Flush();
            fs.Close();
        }
        //private void storeIntToTxt(byte[] shuzu, string path)
        //{
        //    FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);
        //    StreamWriter sw = new StreamWriter(fs);
        //    sw.WriteLine();
        //    sw.WriteLine("------------------------------------------------------------------");
        //    sw.WriteLine(DateTime.Now.ToString());

        //    //开始写入
        //    for (int i = 0; i < shuzu.Length; i++)
        //    {
        //        //for (int j = 0; j < 100; j++)
        //        //{
        //        sw.Write(shuzu[i].ToString("x") + " ");
        //        //}
        //    }
        //    //清空缓冲区
        //    sw.Flush();
        //    //关闭流  
        //    sw.Close();
        //}
        /// <summary>
        /// 方法：读取门限数据
        /// </summary>
        /// <param name="nBoardIndex">板卡编号</param>
        /// <param name="CA_CFAR_RatioArray_Addr">CA地址</param>
        /// <param name="Ca_Write_Array">CA写入地址</param>
        /// <param name="dataLength">数据长度</param>
        /// <returns>返回成功和失败</returns>
        public bool DevWriteCA(uint nBoardIndex, uint CA_CFAR_RatioArray_Addr, uint[] Ca_Write_Array, int dataLength)
        {
            int bLength = dataLength * 4;
            try
            {
                 if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        //write data 
                        byte[] writeData = new byte[bLength];
                        //构造写入数组
                        for (int i = 0; i < dataLength; i++)
                        {
                            byte[] tmp = BitConverter.GetBytes(Ca_Write_Array[i]);
                            tmp.CopyTo(writeData, i * 4);
                        }

                        //send cmd
                        byte[] byte_array_addr = BitConverter.GetBytes(CA_CFAR_RatioArray_Addr);
                        byte[] byte_Length = BitConverter.GetBytes(bLength);
                        DATA_TYPE_INT.CopyTo(NETCMD_WRITE_CA, NETCMD_DATA_TYPE_START);
                        byte_Length.CopyTo(NETCMD_WRITE_CA, NETCMD_DATA_LENGTH_START);
                        byte_array_addr.CopyTo(NETCMD_WRITE_CA, NETCMD_DATA_ADDR_START);
                        byte[] byte_msg_crc = CRC16(writeData);
                        byte_msg_crc.CopyTo(NETCMD_WRITE_CA, NETCMD_RESERVED_START);
                        stream.Write(CRC_ENCODE(NETCMD_WRITE_CA), 0, NETCMD_LENGTH);
                        printByte(NETCMD_WRITE_CA);
                        stream.Flush();
                        //rec ack and get datalength
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_WRITE_SUCCESS, ref ret_cmd_ack_result)) { d("DevWriteCA CMD 错误"); return false; }

                       

                        for (int i = bLength; i > 0; i -= STREAM_BUFFER_LENGTH)
                        {
                            Thread.Sleep(10);
                            int one_lenth = STREAM_BUFFER_LENGTH;
                            if (i > 0 && i < STREAM_BUFFER_LENGTH) one_lenth = i;
                            stream.Write(writeData, bLength - i, one_lenth);
                        }
                        stream.Flush();

                        byte[] recvEndACK = new byte[NETCMD_LENGTH];
                        ret = stream.Read(recvEndACK, 0, NETCMD_LENGTH);
                        printByte(recvEndACK);
                        byte[] byte_ret_msg_crc = new byte[2];
                        byte_ret_msg_crc[0] = recvEndACK[NETCMD_RESERVED_START];
                        byte_ret_msg_crc[1] = recvEndACK[NETCMD_RESERVED_START+1];
                        if (!byteIsEqual(byte_ret_msg_crc, byte_msg_crc)) { d("DevWriteCA 摘要 错误"); return false; }
                        //第二次返回
                        if (!CRC_DECODE(recvEndACK, NETCMD_RET_WRITE_END, ref ret_cmd_ack_result)) { d("DevWriteCA END 错误"); return false; }
                    }
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 方法：写入门限数据
        /// </summary>
        /// <param name="nBoardIndex">板卡编号</param>
        /// <param name="CA_CFAR_RatioArray_Addr">CA地址</param>
        /// <param name="Ca_Write_Array">CA写入地址</param>
        /// <param name="dataLength">数据长度</param>
        /// <returns>返回成功和失败</returns>
        public bool DevWritSpecial(uint nBoardIndex, byte[] SPECIAL_NAME, uint VALUE)
        {//NETCMD_SPECIAL_NAME_SINGLECOL
            try
            {
                 if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    lock (sync)
                    {
                        //send cmd
                        byte[] byte_array_addr = SPECIAL_NAME;
                        //构造写入数组
                        byte[] writeData = BitConverter.GetBytes(VALUE);
                        writeData.CopyTo(NETCMD_WRITE_SPECIAL, 12);
                        byte_array_addr.CopyTo(NETCMD_WRITE_SPECIAL, 8);
                        stream.Write(CRC_ENCODE(NETCMD_WRITE_SPECIAL), 0, NETCMD_LENGTH);
                        printByte(NETCMD_WRITE_SPECIAL);
                        stream.Flush();
                        //rec ack and get datalength
                        byte[] recvACK = new byte[NETCMD_LENGTH];
                        int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                        printByte(recvACK);
                        byte[] ret_cmd_ack_result = new byte[2];
                        if (!CRC_DECODE(recvACK, NETCMD_RET_SPECIAL_SUCCESS, ref ret_cmd_ack_result)) { d("DevWriteSPECIAL CMD 错误"); return false; }
                    }
                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }

            return true;
        }

        // 读取数据，采用模板，pBuffer可用模板
        //public bool DevRead<T>(uint nBoardIndex, ref T[] pBuffer, ref int dLength,byte[] data_type)
        public bool DevRead(uint nBoardIndex, ref int[] pBuffer, ref int dLength, byte[] data_type)
        {
            try
            {
                 if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    data_type.CopyTo(NETCMD_READ, NETCMD_DATA_TYPE_START);
                    //send cmd
                    stream.Write(CRC_ENCODE(NETCMD_READ), 0, NETCMD_LENGTH);
                    stream.Flush();
                    //rec ack and get datalength
                    byte[] recvACK = new byte[NETCMD_LENGTH];
                    int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                    byte[] ret_cmd_ack_result = new byte[2];
                    if (!CRC_DECODE(recvACK, NETCMD_RET_READ_SUCCESS, ref ret_cmd_ack_result)) { d("DevRead 错误"); return false; }
                    //dLength先按 16位进制的数量 理解
                    dLength = (int)recvACK[NETCMD_DATA_LENGTH_START + 3] << 24;
                    dLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 2] << 16;
                    dLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 1] << 8;
                    dLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 0];
                    //recv data 
                    byte[] recvData = new byte[dLength * 2];
                    stream.Read(recvData, 0, dLength);
                    stream.Flush();
                    for (int i = 0; i < dLength; i++)
                    {
                        pBuffer[i] = BitConverter.ToInt16(recvData, i * 2);
                    }

                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }
            return true;
        }


        // 写入数据，采用模板，pBuffer可用模板
        //public bool DevRead<T>(uint nBoardIndex, ref T[] pBuffer, ref int dLength,byte[] data_type)
        public bool DevWrite(uint nBoardIndex, ref int[] pBuffer, ref int nLength, byte[] data_type)
        {
            try
            {
                if (!client.Connected) {  DevTCP(nBoardIndex,   DSP_PORT); }
                {
                    data_type.CopyTo(NETCMD_WRITE, NETCMD_DATA_TYPE_START);
                    byte[] byte_nLength = BitConverter.GetBytes(nLength);
                    byte_nLength.CopyTo(NETCMD_WRITE, NETCMD_DATA_LENGTH_START);
                    //send cmd
                    stream.Write(CRC_ENCODE(NETCMD_WRITE), 0, NETCMD_LENGTH);
                    stream.Flush();
                    //rec ack and get datalength
                    byte[] recvACK = new byte[NETCMD_LENGTH];
                    int ret = stream.Read(recvACK, 0, NETCMD_LENGTH);
                    byte[] ret_cmd_ack_result = new byte[2];
                    if (!CRC_DECODE(recvACK, NETCMD_RET_WRITE_SUCCESS, ref ret_cmd_ack_result)) { d("DevWrite 错误"); return false; }
                    byte[] sendData = new byte[nLength * 4];
                    //dLength先按 16位进制的数量 理解
                    nLength = (int)recvACK[NETCMD_DATA_LENGTH_START + 3] << 24;
                    nLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 2] << 16;
                    nLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 1] << 8;
                    nLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 0];
                    //recv data 
                    byte[] recvData = new byte[nLength * 2];
                    stream.Read(recvData, 0, nLength);
                    stream.Flush();
                    for (int i = 0; i < nLength; i++)
                    {
                        pBuffer[i] = BitConverter.ToInt16(recvData, i * 2);
                    }

                }
            }
            catch (Exception ee)
            {
                Console.WriteLine(ee.Message);
                return false;
            }
            return true;
        }
        //// 写入数据
        //public bool DevWriteBackup(uint nBoardType, uint nBoardIndex, uint nDspIndex, uint uDspAddr, uint nLength, uint nStride, float pBuffer)
        //{
        //    int retAck = -1;

        //    uint[] pSendData = new uint[NETCMD_LENGTH];

        //    int nLengthOfBytes = (int)NETCMD_LENGTH * sizeof(uint) + (int)nLength * 4;
        //    Byte[] CMDData = new Byte[nLengthOfBytes];
        //    byte[] tmp = new byte[sizeof(uint)];
        //    try
        //    {
        //        if (client.Connected)
        //        {
        //            stream = client.GetStream();
        //            for (int i = 0; i < NETCMD_LENGTH; i++)
        //            {
        //                tmp = BitConverter.GetBytes(pSendData[i]);
        //                for (int j = 0; j < sizeof(uint); j++)
        //                    CMDData[i * 4 + j] = tmp[j];
        //            }
        //            for (int i = 0; i < nLength; i++)
        //            {
        //                tmp = BitConverter.GetBytes(pBuffer);
        //                for (int j = 0; j < sizeof(uint); j++)
        //                    CMDData[NETCMD_LENGTH * 4 + i * 4 + j] = tmp[j];
        //            }
        //            stream.Write(CMDData, 0, nLengthOfBytes);

        //            Byte[] nAck = new Byte[4];
        //            Int32 bytes = stream.Read(nAck, 0, nAck.Length);

        //            retAck = BitConverter.ToInt32(nAck, 0);
        //            //自己添加
        //            if (retAck != 0)
        //            {
        //                Console.WriteLine("写入浮点型失败");
        //                Console.WriteLine("retAck=" + retAck);
        //            }
        //            return retAck;
        //        }
        //    }
        //    catch (Exception ee)
        //    {
        //        Console.WriteLine(ee.Message);
        //       return false;
        //    }
        //    return retAck;
        //}

        //public bool DevWrite(uint nBoardType, uint nBoardIndex, uint nDspIndex, uint uDspAddr, uint nLength, uint nStride, int pBuffer)
        //{
        //    int retAck = -1;

        //    uint[] pSendData = new uint[NETCMD_LENGTH];

        //    int nLengthOfBytes = (int)NETCMD_LENGTH * sizeof(uint) + (int)nLength * 4;
        //    Byte[] CMDData = new Byte[nLengthOfBytes];
        //    byte[] tmp = new byte[sizeof(uint)];
        //    try
        //    {
        //        if (client.Connected)
        //        {
        //            stream = client.GetStream();
        //            for (int i = 0; i < NETCMD_LENGTH; i++)
        //            {
        //                tmp = BitConverter.GetBytes(pSendData[i]);
        //                for (int j = 0; j < sizeof(uint); j++)
        //                    CMDData[i * 4 + j] = tmp[j];
        //            }
        //            for (int i = 0; i < nLength; i++)
        //            {
        //                tmp = BitConverter.GetBytes(pBuffer);
        //                for (int j = 0; j < sizeof(uint); j++)
        //                    CMDData[NETCMD_LENGTH * 4 + i * 4 + j] = tmp[j];
        //            }
        //            stream.Write(CMDData, 0, nLengthOfBytes);

        //            Byte[] nAck = new Byte[4];
        //            Int32 bytes = stream.Read(nAck, 0, nAck.Length);

        //            retAck = BitConverter.ToInt32(nAck, 0);
        //            //自己添加
        //            if (retAck != 0)
        //            {
        //                Console.WriteLine("写入整型失败");
        //                Console.WriteLine("retAck = " + retAck);
        //            }
        //            return retAck;
        //        }
        //    }
        //    catch (Exception ee)
        //    {
        //        Console.WriteLine(ee.Message);
        //       return false;
        //    }
        //    return retAck;
        //}

        /// <summary>
        /// 将要发送的命令生成含CRC校验码的16字节命令
        /// </summary>
        /// <param name="NET_CMD"></param>
        /// <returns></returns>
        private byte[] CRC_ENCODE(byte[] NET_CMD)
        {
            byte[] crc_encode = CRC16_NETCMD(NET_CMD);
            crc_encode.CopyTo(NET_CMD, NETCMD_LENGTH - 2);
            return NET_CMD;
        }

        /// <summary>
        /// 将要发送的命令生成含CRC校验码的16字节命令
        /// </summary>
        /// <param name="NET_CMD"></param>
        /// <returns></returns>
        private byte[] CRC_ENCODE_TEMP(byte[] NET_CMD)
        {
            byte[] crc_encode = { 0x78, 0x56 };
            crc_encode.CopyTo(NET_CMD, NETCMD_LENGTH - 2);
            return NET_CMD;
        }
        /// <summary>
        /// CRC校验代码，先计算返回值CRC是否正确，再与标准值比较
        /// </summary>
        /// <param name="ret_cmd">返回值</param>
        /// <param name="NET_CMD_RET">标准值</param>
        /// <returns>如果全部正确返回true,否则返回false</returns>
        private bool CRC_DECODE(byte[] ret_cmd, byte[] NET_CMD_RET, ref byte[] ret_cmd_ack_result)
        {
            byte[] ret_crc = new byte[2];
            ret_crc[0] = ret_cmd[NETCMD_LENGTH - 2];
            ret_crc[1] = ret_cmd[NETCMD_LENGTH - 1];
            byte[] calc_crc = new byte[2];
            calc_crc = CRC16_NETCMD(ret_cmd);
            if (!byteIsEqual(ret_crc, calc_crc))
            {
                Console.WriteLine("CRC校验错误");
                return false;
            }
            //byte[] ret_cmd_ack_result = new byte[2];
            ret_cmd_ack_result[0] = ret_cmd[NETCMD_RET_START];
            ret_cmd_ack_result[1] = ret_cmd[NETCMD_RET_START + 1];
            if (!byteIsEqual(NET_CMD_RET, ret_cmd_ack_result))
            {
                Console.WriteLine("应答结果错误");
                DevTCP(0,  DSP_PORT);
                return false;
            }
            
            return true;
        }
        private void d(string msg)
        {
            Console.WriteLine(msg);
        }
        private byte[] Board(byte[] NET_CMD, uint BoardIndex)
        {
            byte[] BOARD_NET_CMD = new byte[NETCMD_LENGTH];
            return BOARD_NET_CMD;
        }

        /// <summary>
        /// CRC16_NETCMD 
        /// </summary>
        /// <param name="data">要进行计算的数组</param>
        /// <returns>计算后的16位CRC校验码</returns>
        private static byte[] CRC16(byte[] data)
        {
            //byte[] returnVal = new byte[2];
            UInt16 crc_uint = aricheck(data, data.Length);
            byte[] returnVal = BitConverter.GetBytes(crc_uint);
            return returnVal;
        }
        /// <summary>
        /// CRC16_NETCMD 
        /// </summary>
        /// <param name="data">要进行计算的数组</param>
        /// <returns>计算后的16位CRC校验码</returns>
        private static byte[] CRC16_NETCMD(byte[] data)
        {
            //byte[] returnVal = new byte[2];
            UInt16 crc_uint = aricheck(data, data.Length - 2);
            byte[] returnVal = BitConverter.GetBytes(crc_uint);
            return returnVal;
        }

        private static UInt16 aricheck(byte[] data, int length)
        {
            UInt16 encode = 12358; //密钥
            UInt32 i, j;
            UInt16 temp;
            UInt16 check = 0;
            for (j = 0; j < length; j++)
            {
                temp = (UInt16)(data[j] & 0xff);
                for (i = 0; i < 8; i++)
                {
                    if (((UInt16)(temp ^ check) & 0x0001) != 0)
                        check = (UInt16)((UInt16)(check >> 1) ^ encode);
                    else
                        check = (UInt16)(check >> 1);
                    temp = (UInt16)(temp >> 1);
                }

            }
            return check;
        }

        /// <summary>
        /// 比较两个字节数组是否相等
        /// </summary>
        /// <param name="b1">byte数组1</param>
        /// <param name="b2">byte数组2</param>
        /// <returns>是否相等</returns>
        private bool byteIsEqual(byte[] b1, byte[] b2)
        {
            if (b1.Length != b2.Length) return false;
            if (b1 == null || b2 == null) return false;
            for (int i = 0; i < b1.Length; i++)
                if (b1[i] != b2[i]) return false;
            return true;

        }
    }
}

/*
           bLength = (int)recvACK[NETCMD_DATA_LENGTH_START + 3] << 24;
           bLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 2] << 16;
           bLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 1] << 8;
           bLength += (int)recvACK[NETCMD_DATA_LENGTH_START + 0]; 
           */
